package com.example.butterknifedemo;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindColor;
import butterknife.BindDrawable;
import butterknife.BindString;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;

public class MainActivity extends BaseActivity {
    private static final String TAG = "MainActivity";
    /**
     * 绑定资源
     */
    @BindString(R.string.app_name)
    String appName;
    @BindColor(R.color.colorAccent)
    int textColor;
    @BindDrawable(R.mipmap.ic_launcher)
    Drawable drawable;
    @BindDrawable(R.drawable.ic_launcher_background)
    Drawable selector;

    /**
     * 绑定view
     */
    @Nullable @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tv_click_again)
    TextView tvClickAgain;
    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.fl_content)
    FrameLayout flContent;
    /**
     * 装入一个List
     */
    @BindViews({R.id.tv_first, R.id.tv_second})
    List<TextView> textViewList;

    @Override
    public int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void initAllMemersView(Bundle savedInsatanceState) {
        for (TextView textView : textViewList) {
            Log.e(TAG, "initAllMemersView: textStr:" + textView.getText().toString());
        }
    }

    /**
     * 点击事情绑定
     * 方法中的参数可以自定义类型，比如TextView view，那么会自动被转换
     */
    @OnClick(R.id.tv)
    public void onViewClicked(TextView tv) {
        tv.setText("点击了");
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_content, new MyFragment(), "gg");
        fragmentTransaction.commitAllowingStateLoss();
    }

    /**
     * 监听多个View
     */
    @OnClick({R.id.tv, R.id.tv_click_again})
    public void allClick() {
        Log.e(TAG, "allClick: ");
    }

    /**
     * 给EditText添加addTextChangedListener
     */
    @OnTextChanged(value = R.id.et, callback = OnTextChanged.Callback.BEFORE_TEXT_CHANGED)
    void beforeEtChanged(CharSequence s, int start, int ocunt, int after) {
        Log.e(TAG, "beforeEtChanged: s:" + s);
    }

    @OnTextChanged(value = R.id.et, callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onEtChanged(CharSequence s, int string, int before, int count) {
        Log.e(TAG, "onEtChanged: s:" + s);
    }

    @OnTextChanged(value = R.id.et, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void afterEtChanged(Editable s) {
        Log.e(TAG, "afterEtChanged: s:" + s);
    }

    /**
     * 测试统一操作处理
     */
    private void test() {
        ButterKnife.apply(tv, DISABLE);
        ButterKnife.apply(tv, ENABLE, false);
    }

    static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {
        @Override
        public void apply(@NonNull View view, int index) {
            view.setEnabled(false);
        }
    };

    static final ButterKnife.Setter<View, Boolean> ENABLE = new ButterKnife.Setter<View, Boolean>() {
        @Override
        public void set(@NonNull View view, Boolean value, int index) {
            view.setEnabled(value);
        }
    };

}
